/* @(#)GcgSpinnableMenu.java
** 
** Copyright (C) 2012 by Steven D. Stamps
**
**             Trademarks & Copyrights
** Flywheel Management Science(TM), Flywheel Management Model(TM),
** Flywheel Story Editor(TM) and FlywheelMS(TM) are exclusive trademarks
** of Steven D. Stamps and may only be used freely for the purpose of
** identifying the unforked version of this software.  Subsequent forks
** may not use these trademarks.  All other rights are reserved.
**
** DecKanGL (Decorated Kanban Glyph Language) and TribKn (Tribal Knowledge)
** are also exclusive trademarks of Steven D. Stamps.  These may be used
** freely within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** gConGUI (game Controller Graphical User Interface) is an exclusive
** trademark of Steven D. Stamps.  This trademark may be used freely
** within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** Trademark information is available at
** <http://www.flywheelms.com/trademarks>
**
** Flywheel Management Science(TM) is a copyrighted body of management
** metaphors, governance processes, and leadership techniques that is
** owned by Steven D. Stamps.  These copyrighted materials may be freely
** used, without alteration, by the community (users and developers)
** surrounding this GPL3-licensed software.  Additional copyright
** information is available at <http://www.flywheelms.org/copyrights>
**
**              GPL3 Software License
** This program is free software: you can use it, redistribute it and/or
** modify it under the terms of the GNU General Public License, version 3,
** as published by the Free Software Foundation. This program is distributed
** in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
** even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
** PURPOSE.  See the GNU General Public License for more details. You should
** have received a copy of the GNU General Public License, in a file named
** COPYING, along with this program.  If you cannot find your copy, see
** <http://www.gnu.org/licenses/gpl-3.0.html>.
*/
package com.flywheelms.library.gcg.widget;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.flywheelms.library.R;
import com.flywheelms.library.gcg.helper.GcgButtonMenu;

public class GcgSpinnableMenu {
	
	private final Context context;
	private Spinner headingSpinner;
	private LinearLayout menuFrameLayout;
	private int decoratorOrientation;
	private final int[] menuBodyResourceIdArray;
	private final ArrayList<LinearLayout> menuBodyArray = new ArrayList<LinearLayout>();
	private final ArrayList<Drawable> spinnerBackgroundDrawableArray = new ArrayList<Drawable>();
	public static final int DECORATORS_LEFT = 1;
	public static final int DECORATORS_RIGHT = 2;
	private final ArrayList<View> viewBodyArray = new ArrayList<View>();
	
	public GcgSpinnableMenu(
			Context aContext,
			LinearLayout aMenuFrameLayout,
			int aDecoratorOrientation,
			int aMenuHeadingSpinnerResourceId,
			int aMenuHeadingArrayResourceId,
			int[] aMenuBodyResourceIdArray ) {
		this.context = aContext;
		this.menuFrameLayout = aMenuFrameLayout;
		this.decoratorOrientation = aDecoratorOrientation;
		this.headingSpinner = (Spinner) this.menuFrameLayout.findViewById(aMenuHeadingSpinnerResourceId);
		this.menuBodyResourceIdArray = aMenuBodyResourceIdArray;
		spinnerBackgroundSetup();
		menuSetup(aMenuHeadingArrayResourceId);
	}
	
	private void spinnerBackgroundSetup() {
		if(this.decoratorOrientation == DECORATORS_LEFT) {
			switch(this.menuBodyResourceIdArray.length) {
			case 2:
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__1_of_2__spinner_background__left) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__2_of_2__spinner_background__left) );
				break;
			case 3:
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__1_of_3__spinner_background__left) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__2_of_3__spinner_background__left) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__3_of_3__spinner_background__left) );
				break;
			case 4:
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__1_of_4__spinner_background__left) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__2_of_4__spinner_background__left) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__3_of_4__spinner_background__left) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__4_of_4__spinner_background__left) );
				break;
			case 5:
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__1_of_5__spinner_background__left) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__2_of_5__spinner_background__left) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__3_of_5__spinner_background__left) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__4_of_5__spinner_background__left) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__5_of_5__spinner_background__left) );
				break;
			default:
				break;
			}
		} else {
			switch(this.menuBodyResourceIdArray.length) {
			case 2:
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__1_of_2__spinner_background__right) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__2_of_2__spinner_background__right) );
				break;
			case 3:
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__1_of_3__spinner_background__right) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__2_of_3__spinner_background__right) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__3_of_3__spinner_background__right) );
				break;
			case 4:
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__1_of_4__spinner_background__right) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__2_of_4__spinner_background__right) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__3_of_4__spinner_background__right) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__4_of_4__spinner_background__right) );
				break;
			case 5:
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__1_of_5__spinner_background__right) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__2_of_5__spinner_background__right) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__3_of_5__spinner_background__right) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__4_of_5__spinner_background__right) );
				this.spinnerBackgroundDrawableArray.add(
						this.context.getResources().getDrawable(R.drawable.menu_heading__5_of_5__spinner_background__right) );
				break;
			default:
				break;
			}
		}
	}

	protected void menuSetup(int aMenuHeadingArrayResourceId) {
		int theHeadingLayoutResourceId = this.decoratorOrientation == DECORATORS_LEFT ?
				R.layout.spinnable_menu__heading__left : R.layout.spinnable_menu__heading__right;
	    ArrayAdapter<CharSequence> theSpinnerAdapter = ArrayAdapter.createFromResource(
	    		this.context,
	    		aMenuHeadingArrayResourceId,
	    		theHeadingLayoutResourceId);
	    theSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    this.getHeadingSpinnner().setAdapter(theSpinnerAdapter);
	    this.getHeadingSpinnner().setOnItemSelectedListener(new OnItemSelectedListener() {
	    	
	    	@Override
	    	public void onItemSelected(AdapterView<?> aParentView, View aSelectedItemView, int aPosition, long anId) {
	    		processMenuHeader();
	    	}

	    	@Override
	    	public void onNothingSelected(AdapterView<?> arg0) {
	    		return;
	    	} 
	    });
	    
		for(int theMenuBodyResourceId : this.menuBodyResourceIdArray) {
			this.menuBodyArray.add(
					(LinearLayout) this.menuFrameLayout.findViewById(theMenuBodyResourceId) );
		}
		// TODO initialize to saved preferences (when implemented)
	}

	public void processMenuHeader() {
		int theHeadingSelection = getHeadingSelection();
		for(int thePosition = 0; thePosition < this.menuBodyArray.size(); ++thePosition) {
				this.menuBodyArray.get(thePosition).setVisibility(View.INVISIBLE);
		}
		this.menuBodyArray.get(theHeadingSelection).setVisibility(View.VISIBLE);
		this.getHeadingSpinnner().setBackground(this.spinnerBackgroundDrawableArray.get(theHeadingSelection));
		if(this.viewBodyArray.size() > 0) {

			for(int thePosition = 0; thePosition < this.viewBodyArray.size(); ++thePosition) {
					this.viewBodyArray.get(thePosition).setVisibility(View.INVISIBLE);
			}
			this.viewBodyArray.get(theHeadingSelection).setVisibility(View.VISIBLE);
		}
	}
	
	public LinearLayout getMenuFrameLayout() {
		return this.menuFrameLayout;
	}
	
	public int getHeadingSelection() {
		return this.getHeadingSpinnner().getSelectedItemPosition();
	}
	
	public int getActiveMenuIndex() {
		return getHeadingSelection();
	}
	
	public Spinner getHeadingSpinnner() {
		return this.headingSpinner;
	}

	public ArrayList<View> getViewBodyArray() {
		return this.viewBodyArray;
	}

}
